package com.cn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.pojo.Orders;
import com.cn.response.Result;
import com.cn.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * 功能描述: <br>
 *
 * @program: wx-nimi-bank
 * @description: 订单控制类
 * @author: Mr.TYJ
 * @create: 2022-06-13 14:45
 **/
@RestController
@CrossOrigin
@Slf4j
@Api(tags = "订单控制层")
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/getOrdersList")
    public Result getOrdersList(@RequestParam(value = "current",required = true,defaultValue = "1") Integer current,
                                @RequestParam(value = "size",required = true,defaultValue = "3")Integer size,
                                @RequestParam(value = "number",required = false)String number,
                                @RequestParam(value = "status",required = false)Integer status,
                                @RequestParam(value = "phone",required = false)String phone,
                                @RequestParam(value = "consignee",required = false)String consignee
                                ){
        Page<Orders> page=new Page<>(current,size);
        Page<Orders> pageList=ordersService.getOrdersList(number,status,phone,consignee,page);
        return Result.ok().data("ordersList",pageList);
    }

    @GetMapping("/getSelectById")
    public Result getSelectById(@RequestParam(value = "id",required = true)Long id){
        Orders orders=ordersService.selectAllById(id);
        return Result.ok().data("orders",orders);
    }
    @GetMapping("/getSelectId")
    public Result getSelectId(@RequestParam(value = "id",required = true)Long id){
        Orders orders=ordersService.selectById(id);
        return Result.ok().data("orders",orders);
    }

    @ApiModelProperty(value = "删除订单", notes = "通过id删除订单信息")
    @PostMapping("/deleteOrders")
    public Result deleteOrders(@RequestParam("id")Long id){
        int i = ordersService.deleteOrders(id);
        if(i>0)
        {
            return Result.ok().data("code", 200);
        }
        else {
            return Result.ok().data("code",0);
        }

    }

    @ApiModelProperty(value = "查看订单", notes = "通过id查看订单详细信息")
    @GetMapping("/selectOrdersById")
    public Result selectOrdersById(@RequestParam("id")Long id){
        Orders i = ordersService.selectOrdersbyid(id);
        if(i!=null)
        {
            return Result.ok().data("order", i);
        }
        else {
            return Result.ok().data("order",0);
        }
    }

    @ApiModelProperty(value = "修改订单", notes = "通过id删除订单信息")
    @PostMapping("/uodateOrders")
    public Result uodateOrders(@RequestBody Orders orders){
        String result = RandomStringUtils.random(6, false, true);
        orders.setLogisticsNo(result);
        orders.setStatus(3);
        int i = ordersService.updateorder(orders);
        if(i>0)
        {
            return Result.ok().data("code", 200);
        }
        else {
            return Result.ok().data("code",0);
        }

    }
    @ApiModelProperty(value = "修改地址", notes = "通过id修改订单收获地址信息")
    @PostMapping("/updateAddress")
    public Result updateAddress(@RequestBody Orders orders){
        Integer i = ordersService.updateAddress(orders);
        if(i>0)
        {
            return Result.ok().data("code", 200);
        }
        else {
            return Result.ok().data("code",0);
        }

    }
    @ApiModelProperty(value = "修改地址", notes = "通过id修改订单收获地址信息")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam("id")Long id){
        Orders orders=ordersService.selectOrdersbyid(id);
        Integer i = ordersService.updateStatus(orders);
        if(i>0)
        {
            return Result.ok().data("code", 200);
        }
        else {
            return Result.ok().data("code",0);
        }

    }

    @ApiModelProperty(value = "批量删除订单", notes = "通过id批量删除订单信息")
    @PostMapping("/deletebyidlist")
    public Result deletebyidlist(@RequestBody Long[] idlist){
        boolean i = ordersService.removeByIds(Arrays.asList(idlist));
        if(i)
        {
            return Result.ok().data("code",200);
        }
        else {
            return Result.ok().data("code",0);
        }
    }

}
